package com.yonyou.iuap.pub;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.net.ftp.FTPClient;

import com.yonyou.iuap.shell.service.intf.IFtpService;

public class FtpTask implements Runnable{
	private IFtpService ftpService;
	private File file;
	public FtpTask(IFtpService ftpService, File file) {
		super();
		this.ftpService = ftpService;
		this.file = file;
	}
	public void run() {
		if(file.exists()){
			File[] list=file.listFiles();
			if(list.length>0){
//				上传文件
				for(File f:list){
					if(f.isFile()){
						InputStream in = null;
						try {
							in = new FileInputStream(f);
							FTPClient ftpClient = new FTPClient();
							ftpService.uploadFile(ftpClient, f.getName(), in);
						} catch ( Exception e) {
							try {
								in.close();
							} catch (IOException e1) {
								e1.printStackTrace();
							}
							e.printStackTrace();
						} 
					}
				}
//				删除文件
				for(File f:list){
					f.delete();
				}
				file.delete();
			}
		}
	}

}
